package cn.com.controller.frontController;


import cn.com.Enum.SessionAttr;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.dto.DiyWorkArg;
import cn.com.db.model.DiyWork;
import cn.com.db.model.Store;
import cn.com.db.model.StoreEmbryo;
import cn.com.db.model.StoreTaste;
import cn.com.db.service.*;
import cn.com.util.*;
import com.alibaba.fastjson.JSON;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Auth(UserAuth.USER)
@RestController
@RequestMapping("/front/diywork")
public class DiyWorkController {


    @Autowired
    DiyWorkService diyWorkService;
    @Autowired
    StoreService storeService;
    @Autowired
    StoreTasteService storeTasteService;
    @Autowired
    StoreEmbryoService storeEmbryoService;
    @Autowired
    StoreSurfaceService storeSurfaceService;
    @Autowired
    StoreDeckService storeDeckService;
    @Autowired
    StorePartsService storePartsService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createDiyWork(HttpServletRequest request,
                                @RequestParam("store")String store,
                                @RequestParam("name")String name,
                                @RequestParam("photo")String photo,
                                @RequestParam("description")String description,
                                @RequestParam("structure")String structure,
                                @RequestParam("diyWorkArg")String diyWorkArgJson)
    {
        DataResult dataResult=new DataResult();
        if (!storeService.isAlive(store)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("店铺不存在,或店铺不处于接单状态");
            return JsonUtil.toJsonString(dataResult);
        }
        String userAuthingId=(String) SessionUtil.getAttr(request,SessionAttr.USERAUTHINGID);
        DiyWorkArg diyWorkArg=JsonUtil.parseDiyWorkArg(diyWorkArgJson);
        if (diyWorkArg==null){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("diy蛋糕不允许为空");
            return JsonUtil.toJsonString(dataResult);
        }
        if (!storeTasteService.isAlive(diyWorkArg.getTaste())){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("口味不存在或店铺已下架该口味");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreTaste storeTaste=storeTasteService.getStoreTasteById(diyWorkArg.getTaste());
        DiyWork diyWork=new DiyWork();
        diyWork.setId(UidUtil.createUUID());
        diyWork.setUser(userAuthingId);
        diyWork.setName(name);
        diyWork.setPhoto(photo);
        diyWork.setDescription(description);
        diyWork.setStructure(structure);
        diyWork.setCreated_at(TimeUtil.getNowTimestamp());
        diyWork.setUpdated_at(TimeUtil.getNowTimestamp());
        diyWork.setStore(store);
        diyWork.setTaste(storeTaste);
        try {
            diyWorkService.createDiyWork(diyWork,diyWorkArg);
        } catch (Exception e) {
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage(e.getMessage());
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("成功添加diy蛋糕");
        dataResult.setData(diyWorkService.getDiyWorkById(diyWork.getId()));
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/countPrice/{id}/{count}",method = RequestMethod.GET)
    public String countDiyworkPrice(@PathVariable("id")String id,
                                    @PathVariable("count")Integer count)
    {
        DataResult dataResult=new DataResult();
        DiyWork diyWork=diyWorkService.getDiyWorkById(id);
        if (diyWork==null){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("diy蛋糕不存在");
            JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        dataResult.setData(diyWorkService.countDiyWorkPrice(id).multiply(new BigDecimal(count)));
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String getUserDiyWork(HttpServletRequest request,
                                 @RequestParam("page")Integer page,
                                 @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult(page,count);
        String userAuthingId=(String)SessionUtil.getAttr(request,SessionAttr.USERAUTHINGID);
        List<DiyWork> list=diyWorkService.getUserDiyWork(userAuthingId,page,count);
        for (DiyWork diyWork:list){
            Store store=storeService.getStoreById(diyWork.getStore());
            diyWork.setFreight(store.getFreight());
            diyWork.setTotalPrice(diyWorkService.countDiyWorkPrice(diyWork.getId()));
        }
        dataResult.setTotal(diyWorkService.countUserDiyWork(userAuthingId));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取用户diy蛋糕成功");
        dataResult.setData(list);
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getEmbryo/{storeId}",method = RequestMethod.GET)
    public String getEmbryo(@PathVariable("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeEmbryoService.getStoreEmbryo(storeId));
        dataResult.setTotal(storeEmbryoService.countStoreEmbryo(storeId));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getParts",method = RequestMethod.POST)
    public String getParts(@RequestParam(value = "parent") String parent,
                           @RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storePartsService.getStoreParts(storeId,parent));
        dataResult.setTotal(storePartsService.countStoreParts(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getSurface",method = RequestMethod.POST)
    public String getSurface(@RequestParam("storeId")String storeId,
                             @RequestParam(value = "parent") String parent){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeSurfaceService.getStoreSurface(storeId,parent));
        dataResult.setTotal(storeSurfaceService.countStoreSurface(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "getDeck",method = RequestMethod.POST)
    public String getDeck(@RequestParam(value = "parent") String parent,
                          @RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeDeckService.getStoreDeck(storeId,parent));
        dataResult.setTotal(storeDeckService.countStoreDeck(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/getTaste/{storeId}",method = RequestMethod.GET)
    public String getTaste(@PathVariable("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeTasteService.getStoreTaste(storeId));
        dataResult.setTotal(storeTasteService.countStoreTaste(storeId));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

}
